package com.aip;

import com.aip.define.*;

/**
 * A class of variable serialization base on ByteCool.
 * 基于ByteCool之上的变量序列化类.
 * 
 * @contributor amin2312(aminlab@qq.com)
 * @createDate 2014-09-15
 * @updateDate 2014-09-15
 */
public class ByteVar extends ByteCool {
    /**
     * Last affected var type.
     * 最后影响的变量类型.
     */
    public byte VarType;

    /**
     * Constructor.
     * 构造函数.
     */
    public ByteVar(int initSize) {
        super(initSize);
    }

    /**
     * Write a byte variable.
     * 写入一个I8变量.
     */
    public void WriteVarI8(byte value) {
        this.VarType = VAR.I8;
        this.WriteUint8(this.VarType);
        this.WriteInt8(value);
    }

    /**
     * Write a unsigned byte variable.
     * 写入一个U8变量.
     */
    public void WriteVarU8(byte value) {
        this.VarType = VAR.U8;
        this.WriteUint8(this.VarType);
        this.WriteUint8(value);
    }

    /**
     * Write a short variable.
     * 写入一个I16变量.
     */
    public void WriteVarI16(short value) {
        this.VarType = VAR.I16;
        this.WriteUint8(this.VarType);
        this.WriteInt16(value);
    }

    /**
     * Write a unsigned short variable.
     * 写入一个U16变量.
     */
    public void WriteVarU16(short value) {
        this.VarType = VAR.U16;
        this.WriteUint8(this.VarType);
        this.WriteUint16(value);
    }

    /**
     * Write a int variable.
     * 写入一个I32变量.
     */
    public void WriteVarI32(int value) {
        this.VarType = VAR.I32;
        this.WriteUint8(this.VarType);
        this.WriteInt32(value);
    }

    /**
     * Write a unsigned int variable.
     * 写入一个I32变量.
     */
    public void WriteVarU32(int value) {
        this.VarType = VAR.U32;
        this.WriteUint8(this.VarType);
        this.WriteUint32(value);
    }

    /**
     * Write a float variable.
     * 写入一个F32变量.
     */
    public void WriteVarF32(Float value) {
        this.VarType = VAR.F32;
        this.WriteUint8(this.VarType);
        this.WriteFloat32(value);
    }

    /**
     * Write a double variable.
     * 写入一个F64变量.
     */
    public void WriteVarF64(Double value) {
        this.VarType = VAR.F64;
        this.WriteUint8(this.VarType);
        this.WriteFloat64(value);
    }

    /**
     * Write a short error variable.
     * 写入一个E16变量.
     */
    public void WriteVarE16(short value) {
        this.VarType = VAR.E16;
        this.WriteUint8(this.VarType);
        this.WriteUint16(value);
    }

    /**
     * Write a binary variable.
     * 写入一个字节数组.
     */
    public void WriteVarBin(ByteCool value) {
        this.VarType = VAR.BIN;
        this.WriteUint8(this.VarType);
        this.WriteLenBin(value);
    }

    /**
     * Write a utf8 string variable.
     * 写入一个字符串.
     */
    public void WriteVarUTF8(String value) {
        this.VarType = VAR.UTF8;
        this.WriteUint8(this.VarType);
        this.WriteLenUTF8(value);
    }

    /**
     * Write a array of bytes variable.
     * 写入多个字节.
     */
    public void WriteVarBytes(byte[] value) {
        this.VarType = VAR.BYTES;
        this.WriteUint8(this.VarType);
        this.WriteLenBytes(value);
    }

    /**
     * Read a variable depend on the byte of variable type.
     * 读取一个变量.
     */
    public Object ReadVar() {
        if (this.GetBytesReadable() == 0) {
            return null;
        }
        this.VarType = this.ReadUint8();
        if (this.VarType == VAR.I8) {
            return this.ReadInt8();
        } else if (this.VarType == VAR.U8) {
            return this.ReadUint8();
        } else if (this.VarType == VAR.I16) {
            return this.ReadInt16();
        } else if (this.VarType == VAR.U16) {
            return this.ReadUint16();
        } else if (this.VarType == VAR.I32) {
            return this.ReadInt32();
        } else if (this.VarType == VAR.U32) {
            return this.ReadUint32();
        } else if (this.VarType == VAR.F32) {
            return this.ReadFloat32();
        } else if (this.VarType == VAR.F64) {
            return this.ReadFloat64();
        } else if (this.VarType == VAR.E16) {
            return this.ReadInt16();
        } else {
            if (this.VarType == VAR.STR) {
                return this.ReadLenUTF8();
            } else if (this.VarType == VAR.BYTES) {
                return this.ReadLenBytes();
            } else if (this.VarType == VAR.BIN) {
                return this.ReadLenBin();
            } else
                System.out.println("unknown var type:" + this.VarType);
        }
        return null;
    }
}